package com.tanhua.dubbo.server.service;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;

import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;

import com.tanhua.common.vo.ErrorResult;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.UserLikeApi;
import com.tanhua.dubbo.server.api.VideoApi;

import com.tanhua.dubbo.server.enums.CommentType;
import com.tanhua.dubbo.server.pojo.Video;


import com.tanhua.dubbo.server.vo.ManageVideoVo;
import com.tanhua.dubbo.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ManagerVideoService {
    @Autowired
    private UserInfoService userInfoService;

    @Reference
    private VideoApi videoApi;
    @Reference
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoMapper userInfoMapper;



    //查询指定用户id的视频列表
    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid, String sortProp, String sortOrder) {
        //设置分页
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        //传入指定参数进行查询视频列表,返回集合
        List<Video> videoList = videoApi.findByUserId(uid, page, pagesize, sortProp, sortOrder);
        //异常处理
        if (ObjectUtil.isEmpty(videoList)) {
             ErrorResult.findVideoError();
        }
        //创建空集合存储ManageVideoVo对象
        List<ManageVideoVo> videoVoList = new ArrayList<>();
        //遍历视频的信息;并封装vo对象
        for (Video video : videoList) {
            ManageVideoVo manageVideoVo = new ManageVideoVo();      //创建ManageVideoVo对象
            manageVideoVo.setVideoUrl(video.getVideoUrl());       //视频保存地址
            manageVideoVo.setPicUrl(video.getPicUrl());         //封面保存地址
            manageVideoVo.setId(video.getVid().intValue());     //视频id
            manageVideoVo.setCommentCount(Convert.toInt(quanZiApi.queryCount(video.getId().toString(), CommentType.COMMENT))); // 评论数
            manageVideoVo.setLikeCount(Convert.toInt(quanZiApi.queryCount(video.getId().toString(), CommentType.LIKE)));//喜欢数量
            manageVideoVo.setCreateDate(Convert.toLong(video.getCreated()));//创建时间
            manageVideoVo.setReportCount(1);//举报数
            manageVideoVo.setForwardingCoun(1);//转发数
            pageResult.setCounts(videoList.size());//视频数量
            //查询UserInfo信息
            UserInfo userInfo = userInfoMapper.selectById(uid);
            manageVideoVo.setUserId(userInfo.getUserId().intValue());//发布者id
            manageVideoVo.setNickname(userInfo.getNickName());//昵称
            //添加videovo到集合中
            videoVoList.add(manageVideoVo);

        }

        //封装集合信息
        pageResult.setItems(videoVoList);
        //返回结果
        return pageResult;
    }


}
